#!/usr/bin/python
import datetime
import pcapy
import sys
from impacket.ImpactDecoder import *

def usage():
    print "Usage: " + sys.argv[0] + " <interface> <pcap filter> <protocol file>"
    exit()

def main():
    if(len(sys.argv) != 4):
        usage()
    cap = pcapy.open_live(sys.argv[1], 100, 1, 0)
    cap.setfilter(sys.argv[2])
    (header, payload) = cap.next()

    while header:
        x = ICMPDecoder().decode(payload)
        payloadData = x.get_data_as_string()
        print payloadData
        protoOut = open(sys.argv[3],"a")
        protoOut.write(payloadData)
        protoOut.write("\n")
        protoOut.close()
        (header, payload) = cap.next()

main()
